<?php
class FavoriteLogic {
	private static $table = "favorite";
	/**
	 * 得到用户收藏的菜谱
	 *
	 * @param unknown_type $actorId
	 * @param unknown_type $num
	 * @return unknown
	 */
	static function getActorFavs($actorId, $num=50) {
		$sql = "select recipe_id,create_date from ".self::$table." where actor_id=".$actorId." order by create_time desc limit ".$num;
		$db = DBMgr::getSlaver($table);
		return $db->query($sql)->asArray();
	}
	
	/**
	 * 添加收藏
	 *
	 * @param unknown_type $actorId
	 * @param unknown_type $recipeId
	 * @param unknown_type $tags
	 * @param boolean $private 是否隐藏消息
	 * @return unknown 收藏是否成功
	 */
	static function addFavorite($actorId, $recipeId, $private = false) {
		$createTime = time();
		require_once 'Xiachufang/logic/Tag.php';
		
		$sql = "insert ignore into ".self::$table."(actor_id,recipe_id,create_time) values(?,?,?)";
		$db = DBMgr::getMaster(self::$table);
		$db->execute($sql, array($actorId, $recipeId, $createTime));
		
		// 添加事件
		require_once 'Xiachufang/logic/Event.php';
		EventLogic::addEvent(EventLogic::FAV_RECIPE_EVENT, $recipeId, $recipeId, $actorId, $createTime, $private);
	}
	/**
	 * 删除收藏
	 *
	 * @param unknown_type $actorId
	 * @param unknown_type $recipeId
	 * @return unknown
	 */
	static function deleteFavorite($actorId, $recipeId) {
		$sql = "delete from ".self::$table." where actor_id=".$actorId." and recipe_id=".$recipeId;
		$db = DBMgr::getMaster(self::$table);
		if (!$db->delete($sql)) return;
		
		require_once 'Xiachufang/logic/Event.php';
		EventLogic::deleteEvent(EventLogic::FAV_RECIPE_EVENT, $recipeId, $recipeId,$actorId);
	}
}
?>